// Copyright 2011 Google Inc. All Rights Reserved.
// Author: jacobsa@google.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * Register an action with flame. See action.js for documentation on what an
 * action is and what properties it has.
 *
 * @param {!flame.Action} action
 *     The action to register.
 */
flame.registerAction = function(action) {
  var fi = flame.internal;

  // Make sure an action with this name hasn't already been registered.
  var name = action.name;
  if (flame.internal.registeredActions.contains(name)) {
    throw new Error(fi.sprintf('Action %s already registered.', name));
  }

  // Make sure that this outputs of this action haven't already been claimed by
  // another action.
  for (var i = 0; i < action.outputs.length; ++i) {
    var output = action.outputs[i];
    if (fi.outputFileIndex.contains(output)) {
      throw new Error(
          fi.sprintf(
              'Output %s registered by both %s and %s.',
              output,
              fi.outputFileIndex.get(output).name,
              action.name));
    }
  }

  // Store the action in our global map.
  fi.registeredActions.insert(name, action);

  // Index the action by its outputs.
  for (var i = 0; i < action.outputs.length; ++i) {
    fi.outputFileIndex.insert(action.outputs[i], action);
  }
};
